var nodeNormal       = getNode("normal");
var nodeNormalBase   = getChildNode(nodeNormal, "normalBase");
var nodeFocus        = getNode("focus");
var nodeFocusBase    = getChildNode(nodeFocus, "focusBase");
var nodeActive       = getNode("active");
var nodeActiveBase   = getChildNode(nodeActive, "activeBase");
var nodeTitle_active = getChildNode(nodeActive, "titleStr");
var nodeTitle_focus  =  getChildNode(nodeFocus, "titleStr");

var mode = 0; // 0:normal, 1:focus, 2:active

var xmlhttp = null;

function onLoad() {
    loadImage(nodeNormalBase, "./parts/normal.png");
    loadImage(nodeFocusBase,  "./parts/normal.png");
    loadImage(nodeActiveBase, "./parts/normal.png");
}

function onUpKey() {
}

function onDownKey() {
}

function onRightKey() {
}

function onLeftKey() {
}

function onConfirmKey(type) {
    if(mode == 2) {
	var keyword = prompt("Search Keyword", "", true);
	doHttpRequest( keyword );
	//execPhotoPlayer( photo, 0 );
    }
}

function onFocus() {
    mode = 1;

    setStr( nodeTitle_focus, "決定ボタンを押して下さい" );

    setVisible(nodeNormal, 0);
    setVisible(nodeFocus, 1);
    setVisible(nodeActive, 0);
}

function onUnfocus() {
    mode = 0;

    setVisible(nodeNormal, 1);
    setVisible(nodeFocus, 0);
    setVisible(nodeActive, 0);
}

function onActivate() {
    mode = 2;

    setStr( nodeTitle_active, "決定ボタンを押して下さい" );

    setVisible(nodeNormal, 0);
    setVisible(nodeFocus, 0);
    setVisible(nodeActive, 1);
}

function onReadyStateChangeCallback()
{
    print("onreadystatechange " + xmlhttp.readyState + "\n");

    if( xmlhttp.readyState != 4 ) {
	print("readyState = " + xmlhttp.readyState + "\n");
	return;
    } else if( xmlhttp.status != 200 ) {
	print("ERROR : HTTP status " + xmlhttp.status + "\n");
	return;
    }

    parseRss(xmlhttp.responseXML);
}

function doHttpRequest( keyword )
{
    print("doHttpRequest()\n");
    
    xmlhttp = new XMLHttpRequest();
    if( !xmlhttp ) {
	print("error 1\n");
	return;
    }

    xmlhttp.onreadystatechange = onReadyStateChangeCallback;
    //xmlhttp.open('GET', 'http://api.flickr.com/services/feeds/photos_public.gne?id=38701746@N00&lang=en-us&format=rss_200');
    xmlhttp.open('GET', 'http://pipes.yahoo.com/pipes/pipe.run?_id=egvqSpKh3RGo2z0Zbrsjiw&_render=rss&urlinput1=' + keyword );
    xmlhttp.send(null);
}

function parseRss(xml)
{
    print("parseRss()\n");

    var items = xml.getElementsByTagName("item");
    var item_cnt = 0;
    var photo = new Array;

    if( !items ) {
	print("error !channel\n");
	return;
    }

    if( 50 < items.length ) {
	item_cnt = 50;
    } else {
	item_cnt = items.length;
    }

    for( var i=0; i<item_cnt; i++ ) {
	var item = items[i].getElementsByTagName( 'media:content' );

	photo[i] = item[0].getAttribute( 'url' );
	photo[i] = photo[i].replace( '_m.jpg', '.jpg' );

	photo[i] = new PhotoContent( photo[i] );

	//print(photo[i] + "\n");
	//setStr(nodeTitle, photo[i]);
    }
    execPhotoPlayer( photo, 1 );
}

